<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<link th:href="@{/ajax/libs/jquery-layout/jquery.layout-latest.css}" rel="stylesheet"/>
<link th:href="@{/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css}" rel="stylesheet"/>
<body class="gray-bg">
<div class="ui-layout-west">
    <div class="main-content">
        <div class="box box-main">
            <div class="box-header">
                <div class="box-title">
                    <i class="fa icon-grid"></i> 产品版本
                </div>
                <div class="box-tools pull-right">
                    <a type="button" class="btn btn-box-tool menuItem" href="#" onclick="toProductVersion()"
                       title="管理产品版本"><i class="fa fa-edit"></i></a>
                    <button type="button" class="btn btn-box-tool" id="btnExpand" title="展开" style="display:none;"><i
                            class="fa fa-chevron-up"></i></button>
                    <button type="button" class="btn btn-box-tool" id="btnCollapse" title="折叠"><i
                            class="fa fa-chevron-down"></i></button>
                    <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新产品版本"><i
                            class="fa fa-refresh"></i></button>
                </div>
            </div>
            <div class="ui-layout-content">
                <div id="tree" class="ztree"></div>
            </div>
        </div>
    </div>
</div>
<div class="container-div  ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <table class="table table-hover no-margins" id="resource-download-table">
                <thead>
                <tr>
                    <th>产品</th>
                    <th>版本</th>
                    <th>名称</th>
                    <th>全量/增量</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<script th:src="@{/ajax/libs/jquery-layout/jquery.layout-latest.js}"></script>
<script th:src="@{/ajax/libs/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js}"></script>
<script th:inline="javascript">
    var prefix = ctx + "system/productResource";

    $(function () {
        $('body').layout({west__size: 185});
        queryResourceList();
        queryProductVersionTree();
    });

    function queryResourceList(productVersionId) {
        var requestData = {};
        requestData.resourceType ="script";
        //若参数为空，则不添加
        if(typeof productVersionId != "undefined" && productVersionId != null && productVersionId != ""){
            requestData.productVersionId =productVersionId;
        }
        $.ajax({
            cache : true,
            type : "POST",
            url : prefix + "/list",
            data : requestData,
            async : false,
            error : function(request) {
                $.modal.alertError("系统错误");
            },
            success : function(data) {
                if(typeof data != "undefined" && data != null){
                    var prList = data.rows;
                    //渲染table
                    $("#resource-download-table tbody").empty();
                    var trHtml = "";
                    prList.forEach(function(productResource){
                        var incrementText = "全量";
                        if(productResource.isIncrement=="1"){
                            incrementText = "增量";
                        }
                        trHtml = trHtml + "<tr><td>" +productResource.productVersion.productName + "</td>";
                        trHtml = trHtml + "<td>" +productResource.version + "</td>";
                        // trHtml = trHtml + "<td>" +productResource.systemType + "</td>";
                        trHtml = trHtml + "<td>" +productResource.resourceName+"</td>";
                        trHtml = trHtml + "<td>" +incrementText+"</td>";
                        trHtml = trHtml + "<td><a class=\"btn btn-primary download\" product-resource-id=\"" +productResource.id+"\">"+"下载" + "</a></td></tr>";
                    });
                    $("#resource-download-table tbody").html(trHtml);
                }
                // $.operate.successCallback(data);
            }
        });
    };

    function queryProductVersionTree() {
        var url = ctx + "system/productVersion/treeData";
        var options = {
            url: url,
            expandLevel: 2,
            onClick: zOnClick
        };
        $.tree.init(options);

        function zOnClick(event, treeId, treeNode) {
            queryResourceList(treeNode.id);
        }
    }

    $('#btnExpand').click(function () {
        $._tree.expandAll(true);
        $(this).hide();
        $('#btnCollapse').show();
    });

    $('#btnCollapse').click(function () {
        $._tree.expandAll(false);
        $(this).hide();
        $('#btnExpand').show();
    });

    $('#btnRefresh').click(function () {
        queryProductVersionTree();
    });

    /*产品版本管理页面*/
    function toProductVersion() {
        var url = ctx + "system/productVersion";
        createMenuItem(url, "产品版本");
    }

    $("body").on("click","a.download",function () {
        var context = getContextPath();
        var downloadUrl = context + "/file/download";
        var productResourceId = $(this).attr("product-resource-id");
        downloadUrl = downloadUrl + "?productResourceId="+productResourceId;
        window.open(downloadUrl);
    })
</script>
</body>
</html>